#
# Copyright 2023 The titan-search Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#[[kmcmake_cc_test(
        NAME custom_comparator_test
        SOURCES custom_comparator_test.cc
        DEPS turbo::turbo ${KMCMAKE_DEPS_LINK} ${GTEST_LIB} ${GTEST_MAIN_LIB}
        COPTS ${USER_CXX_FLAGS}
)]]
#[[
kmcmake_cc_test(
        NAME foo_test
        SOURCES foo_test.cc
        DEPS ${PROJECT}::foo ${KMCMAKE_DEPS_LINK} gtest gtest_main
        COPTS ${USER_CXX_FLAGS}
)

kmcmake_cc_test(
        NAME foo_doctest
        SOURCES foo_doctest.cc
        DEPS ${PROJECT}::foo ${KMCMAKE_DEPS_LINK} doctest
        COPTS ${USER_CXX_FLAGS}
)
]]
find_package(KTest REQUIRED)
find_library(PROFILER_LIB NAMES libprofiler.a)

kmcmake_cc_test(
        NAME kutex_test
        MODULE kthread
        SOURCES kutex_test.cc
        CXXOPTS
        LINKS
        kthread::kthread_static
        KTest::ktest
        KTest::ktest_main
        ${KMCMAKE_DEPS_LINK}
)


kmcmake_cc_test(
        NAME kutex_multi_tag_test
        MODULE kthread
        SOURCES kutex_multi_tag_test.cc
        CXXOPTS
        LINKS
        kthread::kthread_static
        KTest::ktest
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_test(
        NAME cond_bug_test
        MODULE kthread
        SOURCES cond_bug_test.cc
        CXXOPTS
        LINKS
        kthread::kthread_static
        KTest::ktest
        KTest::ktest_main
        ${KMCMAKE_DEPS_LINK}
)
if(PROFILER_LIB)
kmcmake_cc_test(
        NAME cond_test
        MODULE kthread
        SOURCES cond_test.cc
        CXXOPTS
        -fno-access-control
        LINKS
        kthread::kthread_static
        KTest::ktest
        KTest::ktest_main
        ${PROFILER_LIB}
        PkgConfig::libunwind
        ${KMCMAKE_DEPS_LINK}
)
endif ()

kmcmake_cc_test(
        NAME countdown_event_test
        MODULE kthread
        SOURCES countdown_event_test.cc
        CXXOPTS
        LINKS
        kthread::kthread_static
        KTest::ktest
        KTest::ktest_main
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_test(
        NAME futex_test
        MODULE kthread
        SOURCES futex_test.cc
        CXXOPTS
        LINKS
        kthread::kthread_static
        KTest::ktest
        KTest::ktest_main
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_test(
        NAME id_test
        MODULE kthread
        SOURCES id_test.cc
        CXXOPTS
        LINKS
        kthread::kthread_static
        KTest::ktest
        KTest::ktest_main
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_test(
        NAME key_test
        MODULE kthread
        SOURCES key_test.cc
        CXXOPTS
        LINKS
        kthread::kthread_static
        KTest::ktest
        KTest::ktest_main
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_test(
        NAME list_test
        MODULE kthread
        SOURCES list_test.cc
        CXXOPTS
        LINKS
        kthread::kthread_static
        KTest::ktest
        KTest::ktest_main
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_test(
        NAME once_test
        MODULE kthread
        SOURCES once_test.cc
        CXXOPTS
        LINKS
        kthread::kthread_static
        KTest::ktest
        KTest::ktest_main
        ${KMCMAKE_DEPS_LINK}
)
kmcmake_cc_test(
        NAME ping_pong_test
        MODULE kthread
        SOURCES ping_pong_test.cc
        CXXOPTS
        LINKS
        kthread::kthread_static
        KTest::ktest
        KTest::ktest_main
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_test(
        NAME rwlock_test
        MODULE kthread
        SOURCES rwlock_test.cc
        CXXOPTS
        LINKS
        kthread::kthread_static
        KTest::ktest
        KTest::ktest_main
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_test(
        NAME rwlock_check_test
        MODULE kthread
        SOURCES rwlock_test.cc
        DEFINES
        -DCHECK_RWLOCK
        CXXOPTS
        LINKS
        kthread::kthread_static
        KTest::ktest
        KTest::ktest_main
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_test(
        NAME sched_yield_test
        MODULE kthread
        SOURCES sched_yield_test.cc
        DEFINES
        -DCHECK_RWLOCK
        CXXOPTS
        LINKS
        kthread::kthread_static
        KTest::ktest
        KTest::ktest_main
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_test(
        NAME setconcurrency_test
        MODULE kthread
        SOURCES setconcurrency_test.cc
        DEFINES
        -DCHECK_RWLOCK
        CXXOPTS
        LINKS
        kthread::kthread_static
        KTest::ktest
        KTest::ktest_main
        ${KMCMAKE_DEPS_LINK}
)


kmcmake_cc_test(
        NAME timer_thread_test
        MODULE kthread
        SOURCES timer_thread_test.cc
        DEFINES
        -DCHECK_RWLOCK
        CXXOPTS
        LINKS
        kthread::kthread_static
        KTest::ktest
        KTest::ktest_main
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_test(
        NAME work_stealing_queue_test
        MODULE kthread
        SOURCES work_stealing_queue_test.cc
        DEFINES
        -DCHECK_RWLOCK
        CXXOPTS
        LINKS
        kthread::kthread_static
        KTest::ktest
        KTest::ktest_main
        ${KMCMAKE_DEPS_LINK}
)



if(PROFILER_LIB)
    find_package(PkgConfig)
    pkg_check_modules(libunwind REQUIRED IMPORTED_TARGET GLOBAL libunwind)
    kmcmake_cc_test(
            NAME dispatcher_test
            MODULE kthread
            SOURCES dispatcher_test.cc
            CXXOPTS
            -fno-access-control
            LINKS
            kthread::kthread_static
            KTest::ktest
            KTest::ktest_main
            ${PROFILER_LIB}
            PkgConfig::libunwind
            ${KMCMAKE_DEPS_LINK}
    )

    kmcmake_cc_test(
            NAME execution_queue_test
            MODULE kthread
            SOURCES execution_queue_test.cc
            CXXOPTS
            -fno-access-control
            LINKS
            kthread::kthread_static
            KTest::ktest
            KTest::ktest_main
            ${PROFILER_LIB}
            PkgConfig::libunwind
            ${KMCMAKE_DEPS_LINK}
    )

    kmcmake_cc_test(
            NAME mutex_test
            MODULE kthread
            SOURCES mutex_test.cc
            CXXOPTS
            -fno-access-control
            LINKS
            kthread::kthread_static
            KTest::ktest
            KTest::ktest_main
            ${PROFILER_LIB}
            PkgConfig::libunwind
            ${KMCMAKE_DEPS_LINK}
    )
    kmcmake_cc_test(
            NAME kthread_test
            MODULE kthread
            SOURCES kthread_test.cc
            DEFINES
            -DCHECK_RWLOCK
            CXXOPTS
            LINKS
            kthread::kthread_static
            KTest::ktest
            KTest::ktest_main
            ${PROFILER_LIB}
            PkgConfig::libunwind
            ${KMCMAKE_DEPS_LINK}
    )
    kmcmake_cc_test(
            NAME fd_test
            MODULE kthread
            SOURCES fd_test.cc
            DEFINES
            -DCHECK_RWLOCK
            CXXOPTS
            LINKS
            kthread::kthread_static
            KTest::ktest
            KTest::ktest_main
            ${PROFILER_LIB}
            PkgConfig::libunwind
            ${KMCMAKE_DEPS_LINK}
    )
endif ()